package com.atguigu.flink.chapter05.transform;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/14 14:29
 */
public class Flink08_Sum {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        DataStreamSource<WaterSensor> s1 = env.fromElements(
            new WaterSensor("sensor_1", 1L, 10),
            new WaterSensor("sensor_1", 2L, 20),
            new WaterSensor("sensor_1", 5L, 20),
            new WaterSensor("sensor_2", 3L, 30),
            new WaterSensor("sensor_1", 4L, 40)
        );
        // 聚合: 一定是先keyBy才能聚合
        // select id, sum(id)  from t group by id
        // 其他字段: 非聚合字段, 非分组字段, 取的是第一个值
        /*s1
            .keyBy(WaterSensor::getId)
            //.sum("vc")
//            .max("vc")
            //.min("vc")
            .print();*/
    
        s1
            .keyBy(WaterSensor::getId)
//            .maxBy("vc")
            .maxBy("vc", false)
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*

滚动聚合:

    sum max min  其他字段取第一个
    
    maxBy和minBy 其他字段随着最大或最小取值
        当两个值相等的时候, 默认取第一个

注意:
    必须先keyBy再聚合
    聚合的属性必须是数字类型
 */
